package com.sree.tali.dao.homepage.impl;

import com.sree.tali.hibernate.pojo.IpPojo;
import com.sree.tali.hibernate.pojo.UserPojo;
import com.sree.tali.pojo.AdminHomepojo;
import com.sree.tali.pojo.HomeList;
import com.sree.tali.pojo.LoginForm;
import com.sree.tali.pojo.LoginPojo;
import com.sree.tali.util.SecurityAlgo;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.internal.SessionImpl;

public class IndexDao
  implements IIndexDao
{
  public List<HomeList> Load24List(Session paramSession)
  {
    return loadHomeTitles("24hrs", paramSession);
  }
  
  public List<HomeList> Load70List(Session paramSession)
  {
    return loadHomeTitles("70mm", paramSession);
  }
  
  public List<HomeList> AtoZList(Session paramSession)
  {
    return loadHomeTitles("AtoZ", paramSession);
  }
  
  public List<HomeList> loadHomeTitles(String paramString, Session paramSession)
  {
    ArrayList localArrayList = new ArrayList();
    HomeList localHomeList = null;
    try
    {
      Connection localConnection = ((SessionImpl)paramSession).connection();
      String str = "select id ,UPLOAD_TYPE, TITLE from upload_home where UPLOAD_TYPE ='" + paramString + "' order by id DESC";
      Statement localStatement = localConnection.createStatement();
      ResultSet localResultSet = localStatement.executeQuery(str.toUpperCase());
      while (localResultSet.next())
      {
        localHomeList = new HomeList();
        localHomeList.setId(localResultSet.getInt(1));
        localHomeList.setTypr(localResultSet.getString(2));
        localHomeList.setTitle(localResultSet.getString(3));
        localArrayList.add(localHomeList);
      }
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return localArrayList;
  }
  
  public AdminHomepojo DisplayPage(Session paramSession, int paramInt)
  {
    AdminHomepojo localAdminHomepojo = null;
    try
    {
      String str = "from AdminHomepojo pojo  where pojo.id=" + paramInt;
      Query localQuery = paramSession.createQuery(str);
      List localList = localQuery.list();
      localAdminHomepojo = (AdminHomepojo)localList.get(0);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    finally
    {
      if (paramSession != null) {
        paramSession.close();
      }
    }
    return localAdminHomepojo;
  }
  
    public  boolean  Authenticate(Session paramSession, LoginForm form)
    {
    boolean isValidUser=false;
    try
    {
      String str = "from UserPojo pojo  where upper(pojo.userName)='" + form.getJ_username().toUpperCase() +"' and pojo.status = 'A'";
      System.out.println(str);
      Query localQuery = paramSession.createQuery(str);
      List<UserPojo> localList = localQuery.list();
      for(UserPojo loginpojo : localList)
      {
          if(SecurityAlgo.validatePassword(form.getJ_password(), loginpojo.getPassWord()))
          {
              isValidUser= true;
          }
      }
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    finally
    {
      if (paramSession != null) {
        paramSession.close();
      }
    }
         return isValidUser;
     }
    
  @Override
    public  boolean  AuthenticateIp(Session paramSession, String ip )
    {
    boolean isValidUser=false;
    try
    {
      String str = "from IpPojo where status = 'A'";
      System.out.println(str);
      Query localQuery = paramSession.createQuery(str);
      //ip=ip.substring(0, ip.lastIndexOf(".")-1);
      ip=ip.substring(0, ip.indexOf(".", ip.indexOf(".") + 1)+2);
      List<IpPojo> localList = localQuery.list();
      for(IpPojo ippojo : localList)
      {
          if(SecurityAlgo.validatePassword(ip, ippojo.getIp()))
          {
              isValidUser= true;
          }
      }
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    finally
    {
      if (paramSession != null) {
        paramSession.close();
      }
    }
         return isValidUser;
     }


}



